package com.gitee.wsl.unit.weight

import com.gitee.wsl.unit.BaseUnit
import com.gitee.wsl.unit.UnitType

sealed interface WeightUnitType: UnitType<Double> {

    data object G: WeightUnitType {
        override val baseUnit: Double = 1.0
    }

    data object KG: WeightUnitType {
        override val baseUnit: Double = G.baseUnit *1000
    }

    //吨
    data object T: WeightUnitType {
        override val baseUnit: Double =  KG.baseUnit *1000
    }

    //毫克
    data object MG: WeightUnitType {
        override val baseUnit: Double = G.baseUnit *0.001
    }

   //微克
   data object HG: WeightUnitType {
        override val baseUnit: Double = MG.baseUnit *0.001
    }

    //公担
    data object Q: WeightUnitType {
        override val baseUnit: Double = T.baseUnit *0.1
    }

    //磅
    data object BL: WeightUnitType {
        override val baseUnit: Double = G.baseUnit *453.59237
    }

    //盎司
    data object OZ: WeightUnitType {
        override val baseUnit: Double = G.baseUnit *28.349523
    }

    //克拉
    object CT: WeightUnitType {
        override val baseUnit: Double = G.baseUnit * 0.2
    }

    //格令
    data object GR: WeightUnitType {
        override val baseUnit: Double = G.baseUnit * 0.06479891
    }

    //中国古制
    data object 斤: WeightUnitType {
        override val baseUnit: Double = G.baseUnit * 500
    }

    data object 两: WeightUnitType {
        override val baseUnit: Double = 斤.baseUnit * 0.1
    }

    data object 钱: WeightUnitType {
        override val baseUnit: Double = 两.baseUnit * 0.1
    }

}

sealed class WeightUnit(
    value:Double,
    type: WeightUnitType
): BaseUnit<Double, WeightUnitType>(value, type) {

    override fun toBase(): Double {
        return value * type.baseUnit
    }

    override fun convertTo(desType: WeightUnitType):WeightUnit{
        val value = toBase() / desType.baseUnit
        return when(desType){
            WeightUnitType.G -> G(value )
            WeightUnitType.KG -> KG(value )
            WeightUnitType.T -> T(value )
            WeightUnitType.MG -> MG(value )
            WeightUnitType.HG -> HG(value)
            WeightUnitType.Q -> Q(value )
            WeightUnitType.BL -> BL(value)
            WeightUnitType.CT -> CT(value)
            WeightUnitType.GR -> GR(value)
            WeightUnitType.OZ -> OZ(value)
            WeightUnitType.两 -> 两(value)
            WeightUnitType.斤 -> 斤(value)
            WeightUnitType.钱 -> 钱(value)
        }
    }

    class  KG(value:Double): WeightUnit(value, WeightUnitType.KG) {
        override fun format(): String = "${value}KG"
    }

    class  G(value:Double): WeightUnit(value, WeightUnitType.G) {
        override fun format(): String = "${value}G"
    }

    class  T(value:Double): WeightUnit(value, WeightUnitType.T) {
        override fun format(): String = "${value}T"
    }

    class  MG(value:Double): WeightUnit(value, WeightUnitType.MG) {
        override fun format(): String = "${value}MG"
    }

    class  HG(value:Double): WeightUnit(value, WeightUnitType.HG) {
        override fun format(): String = "${value}HG"
    }

    class  Q(value:Double): WeightUnit(value, WeightUnitType.Q) {
        override fun format(): String = "${value}Q"
    }

    class  BL(value:Double): WeightUnit(value, WeightUnitType.BL) {
        override fun format(): String = "${value}BL"
    }

    class  CT(value:Double): WeightUnit(value, WeightUnitType.CT) {
        override fun format(): String = "${value}CT"
    }

    class  GR(value:Double): WeightUnit(value, WeightUnitType.GR) {
        override fun format(): String = "${value}GR"
    }

    class  OZ(value:Double): WeightUnit(value, WeightUnitType.OZ) {
        override fun format(): String = "${value}OZ"
    }

    class 两(value:Double): WeightUnit(value, WeightUnitType.两) {
        override fun format(): String = "${value}两"
    }

    class 钱(value:Double): WeightUnit(value, WeightUnitType.钱) {
        override fun format(): String = "${value}钱"
    }

    class 斤(value:Double): WeightUnit(value, WeightUnitType.斤) {
        override fun format(): String = "${value}斤"
    }
}